import dayjs from 'dayjs'
import _relativeTime from 'dayjs/plugin/relativeTime'
import 'dayjs/locale/zh-cn'
dayjs.locale('zh-cn')
dayjs.extend(_relativeTime)

export const formatDay = (d: Date) => {
  const year = d.getFullYear()
  const month = d.getMonth() + 1
  const day = d.getDate()
  // 2020-01-01

  return [year, month > 9 ? month : '0' + month, day > 9 ? day : '0' + day].join('-')
}

export const heighLight = (str: string, key: string) => {
  // ....
  const reg = new RegExp(key, 'ig')
  // console.dir(reg)
  const newStr = String(str).replace(reg, function (p) {
    // console.log('匹配到的内容是', p)
    return '<span>' + p + '</span>'
  })

  return newStr
}
// todo 封装相对时间函数
// dateStr: 它是一个能被转成Date的时间字符串
// 返回相对时间
export const relativeTime = (dateStr: string) => {
  return dayjs(dateStr).fromNow()
}
